package com.inei.cie.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.inei.cie.entidades.P2_A;
import com.inei.cie.entidades.P2_B;
import com.inei.cie.entidades.P2_B_10N;
import com.inei.cie.entidades.P2_B_11N;
import com.inei.cie.entidades.P2_B_12N;
import com.inei.cie.entidades.P2_B_9N;
import com.inei.cie.entidades.P2_C;
import com.inei.cie.entidades.P2_D;
import com.inei.cie.entidades.P2_D_1N;
import com.inei.cie.entidades.P2_D_3N;
import com.inei.cie.entidades.P2_D_5N;
import com.inei.cie.entidades.P2_D_7N;
import com.inei.cie.entidades.P2_D_9N;
import com.inei.cie.entidades.P2_E;
import com.inei.cie.entidades.P2_F;
import com.inei.cie.entidades.P2_G;
import com.inei.cie.entidades.P2_G_2N;

public class CapituloIIDao {
	private UtilDB util;
	SQLiteDatabase db;
	Cursor cursor = null;

	public CapituloIIDao(Context context) {
		util = new UtilDB(context);
	}

	public void crearP2_A(String id_local, int Nro_Pred) {
		if (!existeP2_A(id_local, Nro_Pred)) {
			db = util.getWritableDatabase();
			db.execSQL("insert into P2_A(id_local,Nro_Pred) values('"
					+ id_local + "'," + Nro_Pred + ")");
		}
		Cerrar();
	}

	public boolean existeP2_A(String id_local, int Nro_Pred) {
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P2_A where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public P2_A obtenerP2_A(String id_local, int Nro_Pred) {
		P2_A obj = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"SELECT * FROM P2_A WHERE id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				obj = new P2_A();
				obj.setId_local(id_local);
				obj.setNro_Pred(Nro_Pred);
				obj.setP2_A_1_Clima(cursor.getInt(cursor
						.getColumnIndex("P2_A_1_Clima")));
				obj.setP2_A_2_Lluv(cursor.getInt(cursor
						.getColumnIndex("P2_A_2_Lluv")));
				obj.setP2_A_2A_Lluv_Mini(cursor.getInt(cursor
						.getColumnIndex("P2_A_2A_Lluv_Mini")));
				obj.setP2_A_2A_Lluv_Mfin(cursor.getInt(cursor
						.getColumnIndex("P2_A_2A_Lluv_Mfin")));
				obj.setP2_A_3_Hel(cursor.getInt(cursor
						.getColumnIndex("P2_A_3_Hel")));
				obj.setP2_A_3A_Hel_Mini(cursor.getInt(cursor
						.getColumnIndex("P2_A_3A_Hel_Mini")));
				obj.setP2_A_3A_Hel_Mfin(cursor.getInt(cursor
						.getColumnIndex("P2_A_3A_Hel_Mfin")));
				obj.setP2_A_4_Gra(cursor.getInt(cursor
						.getColumnIndex("P2_A_4_Gra")));
				obj.setP2_A_4A_Gra_Mini(cursor.getInt(cursor
						.getColumnIndex("P2_A_4A_Gra_Mini")));
				obj.setP2_A_4A_Gra_Mfin(cursor.getInt(cursor
						.getColumnIndex("P2_A_4A_Gra_Mfin")));
				obj.setP2_A_5_Vend(cursor.getInt(cursor
						.getColumnIndex("P2_A_5_Vend")));
				obj.setP2_A_5B_Vend_Mini(cursor.getInt(cursor
						.getColumnIndex("P2_A_5B_Vend_Mini")));
				obj.setP2_A_5B_Vend_Mfin(cursor.getInt(cursor
						.getColumnIndex("P2_A_5B_Vend_Mfin")));
				obj.setP2_A_5A_Vend_Tip(cursor.getInt(cursor
						.getColumnIndex("P2_A_5A_Vend_Tip")));
				boolean estado = cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false;
				obj.setSwCompleto(estado);
				boolean enviado = cursor.getInt(cursor
						.getColumnIndex("SwEnviado")) == 1 ? true : false;
				obj.setSwEnviado(enviado);
				obj.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
				cursor.moveToNext();
			}
		}
		Cerrar();
		return obj;
	}

	public void actualizarP2_A(P2_A obj) {
		db = util.getWritableDatabase();
		ContentValues valores = new ContentValues();
		valores.put("P2_A_1_Clima", obj.getP2_A_1_Clima());
		valores.put("P2_A_2_Lluv", obj.getP2_A_2_Lluv());
		valores.put("P2_A_2A_Lluv_Mini", obj.getP2_A_2A_Lluv_Mini());
		valores.put("P2_A_2A_Lluv_Mfin", obj.getP2_A_2A_Lluv_Mfin());
		valores.put("P2_A_3_Hel", obj.getP2_A_3_Hel());
		valores.put("P2_A_3A_Hel_Mini", obj.getP2_A_3A_Hel_Mini());
		valores.put("P2_A_3A_Hel_Mfin", obj.getP2_A_3A_Hel_Mfin());
		valores.put("P2_A_4_Gra", obj.getP2_A_4_Gra());
		valores.put("P2_A_4A_Gra_Mini", obj.getP2_A_4A_Gra_Mini());
		valores.put("P2_A_4A_Gra_Mfin", obj.getP2_A_4A_Gra_Mfin());
		valores.put("P2_A_5_Vend", obj.getP2_A_5_Vend());
		valores.put("P2_A_5A_Vend_Tip", obj.getP2_A_5A_Vend_Tip());
		valores.put("P2_A_5B_Vend_Mini", obj.getP2_A_5B_Vend_Mini());
		valores.put("P2_A_5B_Vend_Mfin", obj.getP2_A_5B_Vend_Mfin());
		valores.put("Version", obj.getVersion());
		valores.put("SwCompleto", obj.isSwCompleto());
		valores.put("SwEnviado", obj.isSwEnviado());

		int ind = db
				.update("P2_A",
						valores,
						"id_local=? and Nro_Pred=?",
						new String[] { obj.getId_local() + "",
								obj.getNro_Pred() + "" });
		Cerrar();
	}

	public void crearP2_B(String id_local, int Nro_Pred) {
		if (!existeP2_B(id_local, Nro_Pred)) {
			db = util.getWritableDatabase();
			db.execSQL("insert into P2_B(id_local,Nro_Pred) values('"
					+ id_local + "'," + Nro_Pred + ")");
			for (int i = 0; i < 10; i++) {
				db.execSQL("insert into P2_B_9N(id_local,Nro_Pred,P2_B_9_Cod) values('"
						+ id_local + "'," + Nro_Pred + "," + (i + 1) + ")");
			}
			for (int i = 0; i < 7; i++) {
				db.execSQL("insert into P2_B_10N(id_local,Nro_Pred,P2_B_10_Cod) values('"
						+ id_local + "'," + Nro_Pred + "," + (i + 1) + ")");
			}
			for (int i = 0; i < 12; i++) {
				db.execSQL("insert into P2_B_11N(id_local,Nro_Pred,P2_B_11_Cod) values('"
						+ id_local + "'," + Nro_Pred + "," + (i + 1) + ")");
			}
			for (int i = 0; i < 7; i++) {
				db.execSQL("insert into P2_B_12N(id_local,Nro_Pred,P2_B_12_Cod) values('"
						+ id_local + "'," + Nro_Pred + "," + (i + 1) + ")");
			}
		}
		Cerrar();
	}

	public boolean existeP2_B(String id_local, int Nro_Pred) {
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P2_B where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public List<Object> obtenerP2_B(String id_local, int Nro_Pred) {
		P2_B obj = new P2_B();
		List<P2_B_9N> obj9n = new ArrayList<P2_B_9N>();
		List<P2_B_10N> obj10n = new ArrayList<P2_B_10N>();
		List<P2_B_11N> obj11n = new ArrayList<P2_B_11N>();
		List<P2_B_12N> obj12n = new ArrayList<P2_B_12N>();
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"SELECT * FROM P2_B WHERE id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			obj.setId_local(id_local);
			obj.setNro_Pred(Nro_Pred);
			obj.setP2_B_1_Topo(cursor.getInt(cursor
					.getColumnIndex("P2_B_1_Topo")));
			obj.setP2_B_2_Suelo(cursor.getInt(cursor
					.getColumnIndex("P2_B_2_Suelo")));
			obj.setP2_B_2_Suelo_O(cursor.getString(cursor
					.getColumnIndex("P2_B_2_Suelo_O")));
			obj.setP2_B_3_Prof(cursor.getInt(cursor
					.getColumnIndex("P2_B_3_Prof")));
			obj.setP2_B_4_CapAcc(cursor.getString(cursor
					.getColumnIndex("P2_B_4_CapAcc")));
			obj.setP2_B_5_1(cursor.getInt(cursor.getColumnIndex("P2_B_5_1")));
			obj.setP2_B_5_2(cursor.getInt(cursor.getColumnIndex("P2_B_5_2")));
			obj.setP2_B_5_3(cursor.getInt(cursor.getColumnIndex("P2_B_5_3")));
			obj.setP2_B_5A_Uso(cursor.getInt(cursor
					.getColumnIndex("P2_B_5A_Uso")));
			obj.setP2_B_5B_1(cursor.getInt(cursor.getColumnIndex("P2_B_5B_1")));
			obj.setP2_B_5B_2(cursor.getInt(cursor.getColumnIndex("P2_B_5B_2")));
			obj.setP2_B_5B_3(cursor.getInt(cursor.getColumnIndex("P2_B_5B_3")));
			obj.setP2_B_5B_4(cursor.getInt(cursor.getColumnIndex("P2_B_5B_4")));
			obj.setP2_B_6_Trec_H(cursor.getInt(cursor
					.getColumnIndex("P2_B_6_Trec_H")));
			obj.setP2_B_6_Trec_M(cursor.getInt(cursor
					.getColumnIndex("P2_B_6_Trec_M")));
			obj.setP2_B_7_Ttramo_H(cursor.getInt(cursor
					.getColumnIndex("P2_B_7_Ttramo_H")));
			obj.setP2_B_7_Ttramo_M(cursor.getInt(cursor
					.getColumnIndex("P2_B_7_Ttramo_M")));
			obj.setP2_B_8_Pelig(cursor.getInt(cursor
					.getColumnIndex("P2_B_8_Pelig")));
			boolean estado = cursor.getInt(cursor.getColumnIndex("SwCompleto")) == 1 ? true
					: false;
			obj.setSwCompleto(estado);
			boolean enviado = cursor.getInt(cursor.getColumnIndex("SwEnviado")) == 1 ? true
					: false;
			obj.setSwEnviado(enviado);
			obj.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
		}
		cursor = null;
		cursor = db.rawQuery(
				"SELECT * FROM P2_B_9N WHERE id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				P2_B_9N obj9 = new P2_B_9N();
				obj9.setId_local(id_local);
				obj9.setNro_Pred(Nro_Pred);
				obj9.setP2_B_9_Cod(cursor.getInt(cursor
						.getColumnIndex("P2_B_9_Cod")));
				obj9.setP2_B_9_Cod_e(cursor.getInt(cursor
						.getColumnIndex("P2_B_9_Cod_e")));
				boolean estado = cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false;
				obj9.setSwCompleto(estado);
				boolean enviado = cursor.getInt(cursor
						.getColumnIndex("SwEnviado")) == 1 ? true : false;
				obj9.setSwEnviado(enviado);
				obj9.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
				obj9n.add(obj9);
				cursor.moveToNext();
			}
		}
		cursor = null;
		cursor = db.rawQuery(
				"SELECT * FROM P2_B_10N WHERE id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				P2_B_10N obj10 = new P2_B_10N();
				obj10.setId_local(id_local);
				obj10.setNro_Pred(Nro_Pred);
				obj10.setP2_B_10_Cod(cursor.getInt(cursor
						.getColumnIndex("P2_B_10_Cod")));
				obj10.setP2_B_10_Cod_e(cursor.getInt(cursor
						.getColumnIndex("P2_B_10_Cod_e")));
				boolean estado = cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false;
				obj10.setSwCompleto(estado);
				boolean enviado = cursor.getInt(cursor
						.getColumnIndex("SwEnviado")) == 1 ? true : false;
				obj10.setSwEnviado(enviado);
				obj10.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
				obj10n.add(obj10);
				cursor.moveToNext();
			}
		}
		cursor = null;
		cursor = db.rawQuery(
				"SELECT * FROM P2_B_11N WHERE id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				P2_B_11N obj11 = new P2_B_11N();
				obj11.setId_local(id_local);
				obj11.setNro_Pred(Nro_Pred);
				obj11.setP2_B_11_Cod(cursor.getInt(cursor
						.getColumnIndex("P2_B_11_Cod")));
				obj11.setP2_B_11_Cod_e(cursor.getInt(cursor
						.getColumnIndex("P2_B_11_Cod_e")));
				obj11.setP2_B_11_Cod_O(cursor.getString(cursor
						.getColumnIndex("P2_B_11_Cod_O")));
				boolean estado = cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false;
				obj11.setSwCompleto(estado);
				boolean enviado = cursor.getInt(cursor
						.getColumnIndex("SwEnviado")) == 1 ? true : false;
				obj11.setSwEnviado(enviado);
				obj11.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
				obj11n.add(obj11);
				cursor.moveToNext();
			}
		}
		cursor = null;
		cursor = db.rawQuery(
				"SELECT * FROM P2_B_12N WHERE id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				P2_B_12N obj12 = new P2_B_12N();
				obj12.setId_local(id_local);
				obj12.setNro_Pred(Nro_Pred);
				obj12.setP2_B_12_Cod(cursor.getInt(cursor
						.getColumnIndex("P2_B_12_Cod")));
				obj12.setP2_B_12_Cod_e(cursor.getInt(cursor
						.getColumnIndex("P2_B_12_Cod_e")));
				obj12.setP2_B_12_Cod_O(cursor.getString(cursor
						.getColumnIndex("P2_B_12_Cod_O")));
				boolean estado = cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false;
				obj12.setSwCompleto(estado);
				boolean enviado = cursor.getInt(cursor
						.getColumnIndex("SwEnviado")) == 1 ? true : false;
				obj12.setSwEnviado(enviado);
				obj12.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
				obj12n.add(obj12);
				cursor.moveToNext();
			}
		}
		List<Object> lista = new ArrayList<Object>();
		lista.add(obj);
		lista.add(obj9n);
		lista.add(obj10n);
		lista.add(obj11n);
		lista.add(obj12n);
		Cerrar();
		return lista;
	}

	public long actualizarEdificacionA(P2_B obj, List<P2_B_9N> obj9n,
			List<P2_B_10N> obj10n, List<P2_B_11N> obj11n, List<P2_B_12N> obj12n) {
		db = util.getWritableDatabase();
		ContentValues valores = new ContentValues();
		valores.put("P2_B_1_Topo", obj.getP2_B_1_Topo());
		valores.put("P2_B_2_Suelo", obj.getP2_B_2_Suelo());
		valores.put("P2_B_2_Suelo_O", obj.getP2_B_2_Suelo_O());
		valores.put("P2_B_3_Prof", obj.getP2_B_3_Prof());
		valores.put("P2_B_4_CapAcc", obj.getP2_B_4_CapAcc());
		valores.put("P2_B_5_1", obj.getP2_B_5_1());
		valores.put("P2_B_5_2", obj.getP2_B_5_2());
		valores.put("P2_B_5_3", obj.getP2_B_5_3());
		valores.put("P2_B_5A_Uso", obj.getP2_B_5A_Uso());
		valores.put("P2_B_5B_1", obj.getP2_B_5B_1());
		valores.put("P2_B_5B_2", obj.getP2_B_5B_2());
		valores.put("P2_B_5B_3", obj.getP2_B_5B_3());
		valores.put("P2_B_5B_4", obj.getP2_B_5B_4());
		valores.put("P2_B_6_Trec_H", obj.getP2_B_6_Trec_H());
		valores.put("P2_B_6_Trec_M", obj.getP2_B_6_Trec_M());
		valores.put("P2_B_7_Ttramo_H", obj.getP2_B_7_Ttramo_H());
		valores.put("P2_B_7_Ttramo_M", obj.getP2_B_7_Ttramo_M());
		valores.put("P2_B_8_Pelig", obj.getP2_B_8_Pelig());
		valores.put("Version", obj.getVersion());
		valores.put("SwCompleto", obj.isSwCompleto());
		valores.put("SwEnviado", obj.isSwEnviado());
		long id = db
				.update("P2_B",
						valores,
						"id_local=? and Nro_Pred=?",
						new String[] { obj.getId_local() + "",
								obj.getNro_Pred() + "" });

		for (int i = 0; i < obj9n.size(); i++) {
			ContentValues valores9 = new ContentValues();
			valores9.put("P2_B_9_Cod_e", obj9n.get(i).getP2_B_9_Cod_e());
			valores9.put("Version", obj9n.get(i).getVersion());
			valores9.put("SwCompleto", obj9n.get(i).isSwCompleto());
			valores9.put("SwEnviado", obj9n.get(i).isSwEnviado());
			id = db.update("P2_B_9N", valores9,
					"id_local=? and Nro_Pred=? and P2_B_9_Cod=? ",
					new String[] { obj9n.get(i).getId_local() + "",
							obj9n.get(i).getNro_Pred() + "",
							obj9n.get(i).getP2_B_9_Cod() + "" });
		}

		for (int i = 0; i < obj10n.size(); i++) {
			ContentValues valores10 = new ContentValues();
			valores10.put("P2_B_10_Cod_e", obj10n.get(i).getP2_B_10_Cod_e());
			valores10.put("Version", obj10n.get(i).getVersion());
			valores10.put("SwCompleto", obj10n.get(i).isSwCompleto());
			valores10.put("SwEnviado", obj10n.get(i).isSwEnviado());
			id = db.update("P2_B_10N", valores10,
					"id_local=? and Nro_Pred=? and P2_B_10_Cod=? ",
					new String[] { obj10n.get(i).getId_local() + "",
							obj10n.get(i).getNro_Pred() + "",
							obj10n.get(i).getP2_B_10_Cod() + "" });
		}

		for (int i = 0; i < obj11n.size(); i++) {
			ContentValues valores11 = new ContentValues();
			valores11.put("P2_B_11_Cod_e", obj11n.get(i).getP2_B_11_Cod_e());
			valores11.put("P2_B_11_Cod_O", obj11n.get(i).getP2_B_11_Cod_O());
			valores11.put("Version", obj11n.get(i).getVersion());
			valores11.put("SwCompleto", obj11n.get(i).isSwCompleto());
			valores11.put("SwEnviado", obj11n.get(i).isSwEnviado());
			id = db.update("P2_B_11N", valores11,
					"id_local=? and Nro_Pred=? and P2_B_11_Cod=? ",
					new String[] { obj11n.get(i).getId_local() + "",
							obj11n.get(i).getNro_Pred() + "",
							obj11n.get(i).getP2_B_11_Cod() + "" });
		}

		for (int i = 0; i < obj12n.size(); i++) {
			ContentValues valores12 = new ContentValues();
			valores12.put("P2_B_12_Cod_e", obj12n.get(i).getP2_B_12_Cod_e());
			valores12.put("P2_B_12_Cod_O", obj12n.get(i).getP2_B_12_Cod_O());
			valores12.put("Version", obj12n.get(i).getVersion());
			valores12.put("SwCompleto", obj12n.get(i).isSwCompleto());
			valores12.put("SwEnviado", obj12n.get(i).isSwEnviado());
			id = db.update("P2_B_12N", valores12,
					"id_local=? and Nro_Pred=? and P2_B_12_Cod=? ",
					new String[] { obj12n.get(i).getId_local() + "",
							obj12n.get(i).getNro_Pred() + "",
							obj12n.get(i).getP2_B_12_Cod() + "" });
		}

		Cerrar();
		return id;
	}

	public void crearP2_C(String id_local, int Nro_Pred) {
		if (!existeP2_C(id_local, Nro_Pred)) {
			db = util.getWritableDatabase();
			db.execSQL("insert into P2_C(id_local,Nro_Pred) values('"
					+ id_local + "'," + Nro_Pred + ")");
		}
		Cerrar();
	}

	public boolean existeP2_C(String id_local, int Nro_Pred) {
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P2_C where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public P2_C obtenerP2_C(String id_local, int Nro_Pred) {
		P2_C obj = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"SELECT * FROM P2_C WHERE id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				obj = new P2_C();
				obj.setId_local(id_local);
				obj.setNro_Pred(Nro_Pred);
				obj.setP2_C_1Locl_1_Energ(cursor.getInt(cursor
						.getColumnIndex("P2_C_1Locl_1_Energ")));
				obj.setP2_C_1Locl_2_Agua(cursor.getInt(cursor
						.getColumnIndex("P2_C_1Locl_2_Agua")));
				obj.setP2_C_1Locl_3_Alc(cursor.getInt(cursor
						.getColumnIndex("P2_C_1Locl_3_Alc")));
				obj.setP2_C_1Locl_4_Tfija(cursor.getInt(cursor
						.getColumnIndex("P2_C_1Locl_4_Tfija")));
				obj.setP2_C_1Locl_5_Tmov(cursor.getInt(cursor
						.getColumnIndex("P2_C_1Locl_5_Tmov")));
				obj.setP2_C_1Locl_6_Int(cursor.getInt(cursor
						.getColumnIndex("P2_C_1Locl_6_Int")));
				obj.setP2_C_2LocE_1_Energ(cursor.getInt(cursor
						.getColumnIndex("P2_C_2LocE_1_Energ")));
				obj.setP2_C_2LocE_2_Agua(cursor.getInt(cursor
						.getColumnIndex("P2_C_2LocE_2_Agua")));
				obj.setP2_C_2LocE_3_Alc(cursor.getInt(cursor
						.getColumnIndex("P2_C_2LocE_3_Alc")));
				obj.setP2_C_2LocE_4_Tfija(cursor.getInt(cursor
						.getColumnIndex("P2_C_2LocE_4_Tfija")));
				obj.setP2_C_2LocE_5_Tmov(cursor.getInt(cursor
						.getColumnIndex("P2_C_2LocE_5_Tmov")));
				obj.setP2_C_2LocE_6_Int(cursor.getInt(cursor
						.getColumnIndex("P2_C_2LocE_6_Int")));
				boolean estado = cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false;
				obj.setSwCompleto(estado);
				boolean enviado = cursor.getInt(cursor
						.getColumnIndex("SwEnviado")) == 1 ? true : false;
				obj.setSwEnviado(enviado);
				obj.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
				cursor.moveToNext();
			}
		}
		Cerrar();
		return obj;
	}

	public void actualizarP2_C(P2_C obj) {
		db = util.getWritableDatabase();
		ContentValues valores = new ContentValues();
		valores.put("P2_C_1Locl_1_Energ", obj.getP2_C_1Locl_1_Energ());
		valores.put("P2_C_1Locl_2_Agua", obj.getP2_C_1Locl_2_Agua());
		valores.put("P2_C_1Locl_3_Alc", obj.getP2_C_1Locl_3_Alc());
		valores.put("P2_C_1Locl_4_Tfija", obj.getP2_C_1Locl_4_Tfija());
		valores.put("P2_C_1Locl_5_Tmov", obj.getP2_C_1Locl_5_Tmov());
		valores.put("P2_C_1Locl_6_Int", obj.getP2_C_1Locl_6_Int());
		valores.put("P2_C_2LocE_1_Energ", obj.getP2_C_2LocE_1_Energ());
		valores.put("P2_C_2LocE_2_Agua", obj.getP2_C_2LocE_2_Agua());
		valores.put("P2_C_2LocE_3_Alc", obj.getP2_C_2LocE_3_Alc());
		valores.put("P2_C_2LocE_4_Tfija", obj.getP2_C_2LocE_4_Tfija());
		valores.put("P2_C_2LocE_5_Tmov", obj.getP2_C_2LocE_5_Tmov());
		valores.put("P2_C_2LocE_6_Int", obj.getP2_C_2LocE_6_Int());
		valores.put("Version", obj.getVersion());
		valores.put("SwCompleto", obj.isSwCompleto());
		valores.put("SwEnviado", obj.isSwEnviado());

		int ind = db
				.update("P2_C",
						valores,
						"id_local=? and Nro_Pred=?",
						new String[] { obj.getId_local() + "",
								obj.getNro_Pred() + "" });
		Cerrar();
		if (obj.getP2_C_2LocE_1_Energ() == 2) {
			if (existeP2_D(obj.getId_local(), obj.getNro_Pred())) {
				for (int i = 0; i < 4; i++) {
					db = util.getWritableDatabase();
					ContentValues valores3 = new ContentValues();
					valores3.put("P2_D_1_Cod_Est", 0);
					valores3.put("P2_D_1_Cod_O", "");
					valores3.put("SwCompleto", false);
					db.update(
							"P2_D_1N",
							valores3,
							"id_local=? and Nro_Pred=? and P2_D_1_Cod=?",
							new String[] { obj.getId_local() + "",
									obj.getNro_Pred() + "", (i + 1) + "" });
					Cerrar();
				}
			}

			db = util.getWritableDatabase();
			ContentValues valores2 = new ContentValues();
			valores2.put("P2_D_2_Energ_CantSum", 0);
			valores2.put("P2_D_4_Energ_Emp", "");
			db.update("P2_D", valores2, "id_local=? and Nro_Pred=?",
					new String[] { obj.getId_local() + "",
							obj.getNro_Pred() + "" });
			Cerrar();

			db = util.getWritableDatabase();
			db.delete("P2_D_3N", "id_local=? and Nro_Pred=?", new String[] {
					obj.getId_local() + "", obj.getNro_Pred() + "" });
			Cerrar();
		}
	}

	public void crearP2_D(String id_local, int Nro_Pred) {
		if (!existeP2_D(id_local, Nro_Pred)) {
			db = util.getWritableDatabase();
			db.execSQL("insert into P2_D(id_local,Nro_Pred) values('"
					+ id_local + "'," + Nro_Pred + ")");
			for (int i = 0; i < 4; i++) {
				db.execSQL("insert into P2_D_1N(id_local,Nro_Pred,P2_D_1_Cod) values('"
						+ id_local + "'," + Nro_Pred + "," + (i + 1) + ")");
			}
			for (int i = 0; i < 6; i++) {
				db.execSQL("insert into P2_D_5N(id_local,Nro_Pred,P2_D_5_Cod) values('"
						+ id_local + "'," + Nro_Pred + "," + (i + 1) + ")");
			}
			for (int i = 0; i < 7; i++) {
				db.execSQL("insert into P2_D_9N(id_local,Nro_Pred,P2_D_9_Cod) values('"
						+ id_local + "'," + Nro_Pred + "," + (i + 1) + ")");
			}
		}
		Cerrar();
	}

	public boolean existeP2_D(String id_local, int Nro_Pred) {
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P2_D where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public List<Object> obtenerP2_D(String id_local, int Nro_Pred) {
		P2_D obj = new P2_D();
		List<P2_D_1N> obj1n = new ArrayList<P2_D_1N>();
		List<P2_D_5N> obj5n = new ArrayList<P2_D_5N>();
		List<P2_D_9N> obj9n = new ArrayList<P2_D_9N>();
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"SELECT * FROM P2_D WHERE id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			obj.setId_local(id_local);
			obj.setNro_Pred(Nro_Pred);
			obj.setP2_D_2_Energ_CantSum(cursor.getInt(cursor
					.getColumnIndex("P2_D_2_Energ_CantSum")));
			obj.setP2_D_4_Energ_Emp(cursor.getString(cursor
					.getColumnIndex("P2_D_4_Energ_Emp")));
			obj.setP2_D_6_Agua_CantSum(cursor.getInt(cursor
					.getColumnIndex("P2_D_6_Agua_CantSum")));
			obj.setP2_D_8_Agua_Emp(cursor.getString(cursor
					.getColumnIndex("P2_D_8_Agua_Emp")));
			obj.setP2_D_9_Desag(cursor.getInt(cursor
					.getColumnIndex("P2_D_9_Desag")));
			boolean estado = cursor.getInt(cursor.getColumnIndex("SwCompleto")) == 1 ? true
					: false;
			obj.setSwCompleto(estado);
			boolean enviado = cursor.getInt(cursor.getColumnIndex("SwEnviado")) == 1 ? true
					: false;
			obj.setSwEnviado(enviado);
			obj.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
		}
		cursor = null;
		cursor = db.rawQuery(
				"SELECT * FROM P2_D_1N WHERE id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				P2_D_1N obj1 = new P2_D_1N();
				obj1.setId_local(id_local);
				obj1.setNro_Pred(Nro_Pred);
				obj1.setP2_D_1_Cod(cursor.getInt(cursor
						.getColumnIndex("P2_D_1_Cod")));
				obj1.setP2_D_1_Cod_Est(cursor.getInt(cursor
						.getColumnIndex("P2_D_1_Cod_Est")));
				obj1.setP2_D_1_Cod_O(cursor.getString(cursor
						.getColumnIndex("P2_D_1_Cod_O")));
				boolean estado = cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false;
				obj1.setSwCompleto(estado);
				boolean enviado = cursor.getInt(cursor
						.getColumnIndex("SwEnviado")) == 1 ? true : false;
				obj1.setSwEnviado(enviado);
				obj1.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
				obj1n.add(obj1);
				cursor.moveToNext();
			}
		}
		cursor = null;
		cursor = db.rawQuery(
				"SELECT * FROM P2_D_5N WHERE id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				P2_D_5N obj5 = new P2_D_5N();
				obj5.setId_local(id_local);
				obj5.setNro_Pred(Nro_Pred);
				obj5.setP2_D_5_Cod(cursor.getInt(cursor
						.getColumnIndex("P2_D_5_Cod")));
				obj5.setP2_D_5_Cod_Est(cursor.getInt(cursor
						.getColumnIndex("P2_D_5_Cod_Est")));
				obj5.setP2_D_5_Cod_O(cursor.getString(cursor
						.getColumnIndex("P2_D_5_Cod_O")));
				boolean estado = cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false;
				obj5.setSwCompleto(estado);
				boolean enviado = cursor.getInt(cursor
						.getColumnIndex("SwEnviado")) == 1 ? true : false;
				obj5.setSwEnviado(enviado);
				obj5.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
				obj5n.add(obj5);
				cursor.moveToNext();
			}
		}

		cursor = null;
		cursor = db.rawQuery(
				"SELECT * FROM P2_D_9N WHERE id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				P2_D_9N obj9 = new P2_D_9N();
				obj9.setId_local(id_local);
				obj9.setNro_Pred(Nro_Pred);
				obj9.setP2_D_9_Cod(cursor.getInt(cursor
						.getColumnIndex("P2_D_9_Cod")));
				obj9.setP2_D_9_Cod_Est(cursor.getInt(cursor
						.getColumnIndex("P2_D_9_Cod_Est")));
				boolean estado = cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false;
				obj9.setSwCompleto(estado);
				boolean enviado = cursor.getInt(cursor
						.getColumnIndex("SwEnviado")) == 1 ? true : false;
				obj9.setSwEnviado(enviado);
				obj9.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
				obj9n.add(obj9);
				cursor.moveToNext();
			}
		}

		List<Object> lista = new ArrayList<Object>();
		lista.add(obj);
		lista.add(obj1n);
		lista.add(obj5n);
		lista.add(obj9n);
		Cerrar();
		return lista;
	}

	public long actualizarEdificacionD(P2_D obj, List<P2_D_1N> obj1n,
			List<P2_D_5N> obj5n, List<P2_D_9N> obj9n, List<P2_D_3N> obj3n,
			List<P2_D_7N> obj7n) {
		db = util.getWritableDatabase();
		ContentValues valores = new ContentValues();
		valores.put("P2_D_2_Energ_CantSum", obj.getP2_D_2_Energ_CantSum());
		valores.put("P2_D_4_Energ_Emp", obj.getP2_D_4_Energ_Emp());
		valores.put("P2_D_6_Agua_CantSum", obj.getP2_D_6_Agua_CantSum());
		valores.put("P2_D_8_Agua_Emp", obj.getP2_D_8_Agua_Emp());
		valores.put("P2_D_9_Desag", obj.getP2_D_9_Desag());
		valores.put("Version", obj.getVersion());
		valores.put("SwCompleto", obj.isSwCompleto());
		valores.put("SwEnviado", obj.isSwEnviado());
		long id = db
				.update("P2_D",
						valores,
						"id_local=? and Nro_Pred=?",
						new String[] { obj.getId_local() + "",
								obj.getNro_Pred() + "" });

		for (int i = 0; i < obj1n.size(); i++) {
			ContentValues valores1 = new ContentValues();
			valores1.put("P2_D_1_Cod_Est", obj1n.get(i).getP2_D_1_Cod_Est());
			valores1.put("P2_D_1_Cod_O", obj1n.get(i).getP2_D_1_Cod_O());
			valores1.put("Version", obj1n.get(i).getVersion());
			valores1.put("SwCompleto", obj1n.get(i).isSwCompleto());
			valores1.put("SwEnviado", obj1n.get(i).isSwEnviado());
			id = db.update("P2_D_1N", valores1,
					"id_local=? and Nro_Pred=? and P2_D_1_Cod=? ",
					new String[] { obj1n.get(i).getId_local() + "",
							obj1n.get(i).getNro_Pred() + "",
							obj1n.get(i).getP2_D_1_Cod() + "" });
		}

		for (int i = 0; i < obj5n.size(); i++) {
			ContentValues valores5 = new ContentValues();
			valores5.put("P2_D_5_Cod_Est", obj5n.get(i).getP2_D_5_Cod_Est());
			valores5.put("P2_D_5_Cod_O", obj5n.get(i).getP2_D_5_Cod_O());
			valores5.put("Version", obj5n.get(i).getVersion());
			valores5.put("SwCompleto", obj5n.get(i).isSwCompleto());
			valores5.put("SwEnviado", obj5n.get(i).isSwEnviado());
			id = db.update("P2_D_5N", valores5,
					"id_local=? and Nro_Pred=? and P2_D_5_Cod=? ",
					new String[] { obj5n.get(i).getId_local() + "",
							obj5n.get(i).getNro_Pred() + "",
							obj5n.get(i).getP2_D_5_Cod() + "" });
		}

		for (int i = 0; i < obj9n.size(); i++) {
			ContentValues valores9 = new ContentValues();
			valores9.put("P2_D_9_Cod_Est", obj9n.get(i).getP2_D_9_Cod_Est());
			valores9.put("Version", obj9n.get(i).getVersion());
			valores9.put("SwCompleto", obj9n.get(i).isSwCompleto());
			valores9.put("SwEnviado", obj9n.get(i).isSwEnviado());
			id = db.update("P2_D_9N", valores9,
					"id_local=? and Nro_Pred=? and P2_D_9_Cod=? ",
					new String[] { obj9n.get(i).getId_local() + "",
							obj9n.get(i).getNro_Pred() + "",
							obj9n.get(i).getP2_D_9_Cod() + "" });
		}

		if (obj1n.get(0).getP2_D_1_Cod_Est() == 0) {
			db = util.getWritableDatabase();
			ContentValues valores2 = new ContentValues();
			valores2.put("P2_D_2_Energ_CantSum", 0);
			valores2.put("P2_D_4_Energ_Emp", "");
			db.update("P2_D", valores2, "id_local=? and Nro_Pred=?",
					new String[] { obj.getId_local() + "",
							obj.getNro_Pred() + "" });
			Cerrar();

			db = util.getWritableDatabase();
			db.delete("P2_D_3N", "id_local=? and Nro_Pred=?", new String[] {
					obj.getId_local() + "", obj.getNro_Pred() + "" });
			Cerrar();
		} else {
			if (obj3n != null) {
				for (int i = 0; i < obj3n.size(); i++) {
					if (!ExisteP2_D_3N(obj3n.get(i))) {
						db = util.getWritableDatabase();
						ContentValues c = new ContentValues();
						c.put("id_local", obj3n.get(i).getId_local());
						c.put("Nro_Pred", obj3n.get(i).getNro_Pred());
						c.put("P2_D_3_Nro", obj3n.get(i).getP2_D_3_Nro());
						c.put("P2_D_3_SumNro", obj3n.get(i).getP2_D_3_SumNro());
						c.put("P2_D_3_1DocRef", obj3n.get(i)
								.getP2_D_3_1DocRef());
						c.put("SwCompleto", obj3n.get(i).isSwCompleto());
						db.insert("P2_D_3N", null, c);
						db.close();
					} else {
						db = util.getWritableDatabase();
						ContentValues c = new ContentValues();
						c.put("P2_D_3_SumNro", obj3n.get(i).getP2_D_3_SumNro());
						c.put("P2_D_3_1DocRef", obj3n.get(i)
								.getP2_D_3_1DocRef());
						c.put("SwCompleto", obj3n.get(i).isSwCompleto());
						db.update("P2_D_3N", c,
								"id_local=? and Nro_Pred=? and P2_D_3_Nro=?",
								new String[] { obj3n.get(i).getId_local() + "",
										obj3n.get(i).getNro_Pred() + "",
										obj3n.get(i).getP2_D_3_Nro() + "" });
					}
				}
			}
		}

		if (obj5n.get(0).getP2_D_5_Cod_Est() == 0) {
			db = util.getWritableDatabase();
			ContentValues valores5 = new ContentValues();
			valores5.put("P2_D_6_Agua_CantSum", 0);
			valores5.put("P2_D_8_Agua_Emp", "");
			db.update("P2_D", valores5, "id_local=? and Nro_Pred=?",
					new String[] { obj.getId_local() + "",
							obj.getNro_Pred() + "" });
			Cerrar();

			db = util.getWritableDatabase();
			db.delete("P2_D_7N", "id_local=? and Nro_Pred=?", new String[] {
					obj.getId_local() + "", obj.getNro_Pred() + "" });
			Cerrar();
		} else {
			if (obj7n != null) {
				for (int i = 0; i < obj7n.size(); i++) {
					if (!ExisteP2_D_7N(obj7n.get(i))) {
						db = util.getWritableDatabase();
						ContentValues c = new ContentValues();
						c.put("id_local", obj7n.get(i).getId_local());
						c.put("Nro_Pred", obj7n.get(i).getNro_Pred());
						c.put("P2_D_7_Nro", obj7n.get(i).getP2_D_7_Nro());
						c.put("P2_D_7_SumNro", obj7n.get(i).getP2_D_7_SumNro());
						c.put("P2_D_7_1DocRef", obj7n.get(i)
								.getP2_D_7_1DocRef());
						c.put("SwCompleto", obj7n.get(i).isSwCompleto());
						db.insert("P2_D_7N", null, c);
						db.close();
					} else {
						db = util.getWritableDatabase();
						ContentValues c = new ContentValues();
						c.put("P2_D_7_SumNro", obj7n.get(i).getP2_D_7_SumNro());
						c.put("P2_D_7_1DocRef", obj7n.get(i)
								.getP2_D_7_1DocRef());
						c.put("SwCompleto", obj7n.get(i).isSwCompleto());
						db.update("P2_D_7N", c,
								"id_local=? and Nro_Pred=? and P2_D_7_Nro=?",
								new String[] { obj7n.get(i).getId_local() + "",
										obj7n.get(i).getNro_Pred() + "",
										obj7n.get(i).getP2_D_7_Nro() + "" });
					}
				}
			}
		}

		Cerrar();
		return id;
	}

	public boolean ExisteP2_D_3N(P2_D_3N p) {
		db = util.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select * from P2_D_3N where id_local=? and Nro_Pred=? and P2_D_3_Nro=?",
						new String[] { p.getId_local(), p.getNro_Pred() + "",
								p.getP2_D_3_Nro() + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public boolean ExisteP2_D_7N(P2_D_7N p) {
		db = util.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select * from P2_D_7N where id_local=? and Nro_Pred=? and P2_D_7_Nro=?",
						new String[] { p.getId_local(), p.getNro_Pred() + "",
								p.getP2_D_7_Nro() + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public List<P2_D_3N> obtenerP2_D_3N(P2_D obj) {
		Log.i("Suministros", "0");
		List<P2_D_3N> lista = null;
		db = util.getReadableDatabase();

		cursor = db.rawQuery(
				"select * from P2_D_3N where id_local=? and Nro_Pred=?",
				new String[] { obj.getId_local(), obj.getNro_Pred() + "" });
		Log.i("Suministros", "1");
		int i = 2;
		if (cursor.moveToFirst()) {
			Log.i("Suministros", "2");
			lista = new ArrayList<P2_D_3N>();
			while (!cursor.isAfterLast()) {
				Log.i("Suministros", "" + (i++));
				P2_D_3N p = new P2_D_3N();
				p.setId_local(obj.getId_local());
				p.setNro_Pred(obj.getNro_Pred());
				p.setP2_D_3_Nro(cursor.getInt(cursor
						.getColumnIndex("P2_D_3_Nro")));
				p.setP2_D_3_SumNro(cursor.getString(cursor
						.getColumnIndex("P2_D_3_SumNro")));
				p.setP2_D_3_1DocRef(cursor.getInt(cursor
						.getColumnIndex("P2_D_3_1DocRef")));
				p.setSwCompleto((cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false));
				lista.add(p);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return lista;
	}

	public List<P2_D_7N> obtenerP2_D_7N(P2_D obj) {
		List<P2_D_7N> lista = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P2_D_7N where id_local=? and Nro_Pred=?",
				new String[] { obj.getId_local(), obj.getNro_Pred() + "" });
		if (cursor.moveToFirst()) {
			lista = new ArrayList<P2_D_7N>();
			while (!cursor.isAfterLast()) {
				P2_D_7N p = new P2_D_7N();
				p.setId_local(obj.getId_local());
				p.setNro_Pred(obj.getNro_Pred());
				p.setP2_D_7_Nro(cursor.getInt(cursor
						.getColumnIndex("P2_D_7_Nro")));
				p.setP2_D_7_SumNro(cursor.getString(cursor
						.getColumnIndex("P2_D_7_SumNro")));
				p.setP2_D_7_1DocRef(cursor.getInt(cursor
						.getColumnIndex("P2_D_7_1DocRef")));
				p.setSwCompleto((cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false));
				lista.add(p);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return lista;
	}

	public void crearP2_E(String id_local, int Nro_Pred) {
		if (!existeP2_E(id_local, Nro_Pred)) {
			db = util.getWritableDatabase();
			db.execSQL("insert into P2_E(id_local,Nro_Pred) values('"
					+ id_local + "'," + Nro_Pred + ")");
		}
		Cerrar();
	}

	public boolean existeP2_E(String id_local, int Nro_Pred) {
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P2_E where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public P2_E obtenerP2_E(String id_local, int Nro_Pred) {
		P2_E obj = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"SELECT * FROM P2_E WHERE id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				obj = new P2_E();
				obj.setId_local(id_local);
				obj.setNro_Pred(Nro_Pred);
				obj.setP2_E_1_Prayo(cursor.getInt(cursor
						.getColumnIndex("P2_E_1_Prayo")));
				obj.setP2_E_2_Ptierra(cursor.getInt(cursor
						.getColumnIndex("P2_E_2_Ptierra")));
				obj.setP2_E_3_Ano(cursor.getString(cursor
						.getColumnIndex("P2_E_3_Ano")));
				obj.setP2_E_Obs(cursor.getString(cursor
						.getColumnIndex("P2_E_Obs")));
				boolean estado = cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false;
				obj.setSwCompleto(estado);
				boolean enviado = cursor.getInt(cursor
						.getColumnIndex("SwEnviado")) == 1 ? true : false;
				obj.setSwEnviado(enviado);
				obj.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
				cursor.moveToNext();
			}
		}
		Cerrar();
		return obj;
	}

	public void actualizarP2_E(P2_E obj) {
		db = util.getWritableDatabase();
		ContentValues valores = new ContentValues();
		valores.put("P2_E_1_Prayo", obj.getP2_E_1_Prayo());
		valores.put("P2_E_2_Ptierra", obj.getP2_E_2_Ptierra());
		valores.put("P2_E_3_Ano", obj.getP2_E_3_Ano());
		valores.put("P2_E_Obs", obj.getP2_E_Obs());
		valores.put("Version", obj.getVersion());
		valores.put("SwCompleto", obj.isSwCompleto());
		valores.put("SwEnviado", obj.isSwEnviado());

		int ind = db
				.update("P2_E",
						valores,
						"id_local=? and Nro_Pred=?",
						new String[] { obj.getId_local() + "",
								obj.getNro_Pred() + "" });
		Cerrar();
	}

	public void crearP2_F(String id_local, int Nro_Pred) {
		if (!existeP2_F(id_local, Nro_Pred)) {
			db = util.getWritableDatabase();
			for (int i = 0; i < 10; i++) {
				db.execSQL("insert into P2_F(id_local,Nro_Pred,P2_F_1_ElimBas) values('"
						+ id_local + "'," + Nro_Pred + "," + (i + 1) + ")");
			}
		}
		Cerrar();
	}

	public boolean existeP2_F(String id_local, int Nro_Pred) {
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P2_F where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public List<P2_F> obtenerP2_F(String id_local, int Nro_Pred) {
		List<P2_F> obj = new ArrayList<P2_F>();
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"SELECT * FROM P2_F WHERE id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				P2_F obj2 = new P2_F();
				obj2.setId_local(id_local);
				obj2.setNro_Pred(Nro_Pred);
				obj2.setP2_F_1_ElimBas(cursor.getInt(cursor
						.getColumnIndex("P2_F_1_ElimBas")));
				obj2.setP2_F_1_ElimBas_e(cursor.getInt(cursor
						.getColumnIndex("P2_F_1_ElimBas_e")));
				obj2.setP2_F_1_ElimBas_o(cursor.getString(cursor
						.getColumnIndex("P2_F_1_ElimBas_o")));
				boolean estado = cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false;
				obj2.setSwCompleto(estado);
				boolean enviado = cursor.getInt(cursor
						.getColumnIndex("SwEnviado")) == 1 ? true : false;
				obj2.setSwEnviado(enviado);
				obj2.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
				obj.add(obj2);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return obj;
	}

	public void actualizarP2_F(List<P2_F> obj) {
		db = util.getWritableDatabase();
		for (int i = 0; i < obj.size(); i++) {
			ContentValues valores = new ContentValues();
			valores.put("P2_F_1_ElimBas_e", obj.get(i).getP2_F_1_ElimBas_e());
			valores.put("P2_F_1_ElimBas_o", obj.get(i).getP2_F_1_ElimBas_o());
			valores.put("Version", obj.get(i).getVersion());
			valores.put("SwCompleto", obj.get(i).isSwCompleto());
			valores.put("SwEnviado", obj.get(i).isSwEnviado());

			int ind = db.update("P2_F", valores,
					"id_local=? and Nro_Pred=? and P2_F_1_ElimBas=?",
					new String[] { obj.get(i).getId_local() + "",
							obj.get(i).getNro_Pred() + "",
							obj.get(i).getP2_F_1_ElimBas() + "" });
		}
		Cerrar();
	}

	public void crearP2_G(String id_local, int Nro_Pred) {
		if (!existeP2_G(id_local, Nro_Pred)) {
			db = util.getWritableDatabase();
			db.execSQL("insert into P2_G(id_local,Nro_Pred) values('"
					+ id_local + "'," + Nro_Pred + ")");
			for (int i = 0; i < 7; i++) {
				db.execSQL("insert into P2_G_2N(id_local,Nro_Pred,P2_G_2_Nro) values('"
						+ id_local + "'," + Nro_Pred + "," + (i + 1) + ")");
			}
		}
		Cerrar();
	}

	public boolean existeP2_G(String id_local, int Nro_Pred) {
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P2_G where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public List<Object> obtenerP2_G(String id_local, int Nro_Pred) {
		P2_G obj = new P2_G();
		List<P2_G_2N> obj2n = new ArrayList<P2_G_2N>();
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"SELECT * FROM P2_G WHERE id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			obj.setId_local(id_local);
			obj.setNro_Pred(Nro_Pred);
			obj.setP2_G_1_ObEjec(cursor.getInt(cursor
					.getColumnIndex("P2_G_1_ObEjec")));
			obj.setP2_G_Obs(cursor.getString(cursor.getColumnIndex("P2_G_Obs")));
			boolean estado = cursor.getInt(cursor.getColumnIndex("SwCompleto")) == 1 ? true
					: false;
			obj.setSwCompleto(estado);
			boolean enviado = cursor.getInt(cursor.getColumnIndex("SwEnviado")) == 1 ? true
					: false;
			obj.setSwEnviado(enviado);
			obj.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
		}
		cursor = null;
		cursor = db.rawQuery(
				"SELECT * FROM P2_G_2N WHERE id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				P2_G_2N obj2 = new P2_G_2N();
				obj2.setId_local(id_local);
				obj2.setNro_Pred(Nro_Pred);
				obj2.setP2_G_2_Nro(cursor.getInt(cursor
						.getColumnIndex("P2_G_2_Nro")));
				obj2.setP2_G_2_Cod(cursor.getInt(cursor
						.getColumnIndex("P2_G_2_Cod")));
				obj2.setP2_G_2_Otro(cursor.getString(cursor
						.getColumnIndex("P2_G_2_Otro")));
				obj2.setP2_G_2A_EstPre(cursor.getInt(cursor
						.getColumnIndex("P2_G_2A_EstPre")));
				obj2.setP2_G_2B_snip(cursor.getString(cursor
						.getColumnIndex("P2_G_2B_snip")));
				boolean estado = cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false;
				obj2.setSwCompleto(estado);
				boolean enviado = cursor.getInt(cursor
						.getColumnIndex("SwEnviado")) == 1 ? true : false;
				obj2.setSwEnviado(enviado);
				obj2.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
				obj2n.add(obj2);
				cursor.moveToNext();
			}
		}

		List<Object> lista = new ArrayList<Object>();
		lista.add(obj);
		lista.add(obj2n);
		Cerrar();
		return lista;
	}

	public long actualizarEdificacionG(P2_G obj, List<P2_G_2N> obj2n) {
		db = util.getWritableDatabase();
		ContentValues valores = new ContentValues();
		valores.put("P2_G_1_ObEjec", obj.getP2_G_1_ObEjec());
		valores.put("P2_G_Obs", obj.getP2_G_Obs());
		valores.put("Version", obj.getVersion());
		valores.put("SwCompleto", obj.isSwCompleto());
		valores.put("SwEnviado", obj.isSwEnviado());
		long id = db
				.update("P2_G",
						valores,
						"id_local=? and Nro_Pred=?",
						new String[] { obj.getId_local() + "",
								obj.getNro_Pred() + "" });

		for (int i = 0; i < obj2n.size(); i++) {
			ContentValues valores2 = new ContentValues();
			valores2.put("P2_G_2_Cod", obj2n.get(i).getP2_G_2_Cod());
			valores2.put("P2_G_2_Otro", obj2n.get(i).getP2_G_2_Otro());
			valores2.put("P2_G_2A_EstPre", obj2n.get(i).getP2_G_2A_EstPre());
			valores2.put("P2_G_2B_snip", obj2n.get(i).getP2_G_2B_snip());
			valores2.put("Version", obj2n.get(i).getVersion());
			valores2.put("SwCompleto", obj2n.get(i).isSwCompleto());
			valores2.put("SwEnviado", obj2n.get(i).isSwEnviado());
			id = db.update("P2_G_2N", valores2,
					"id_local=? and Nro_Pred=? and P2_G_2_Nro=? ",
					new String[] { obj2n.get(i).getId_local() + "",
							obj2n.get(i).getNro_Pred() + "",
							obj2n.get(i).getP2_G_2_Nro() + "" });
		}

		Cerrar();
		return id;
	}

	protected void Cerrar() {
		cursor.close();
		db.close();
	}
}
